package com.wkbb.sports.utils;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * @author yugaixin
 * @Description http 接口请求工具类
 * @date 2019-09-24
 */
public class HttpRequestUtil {

    private static CloseableHttpClient httpClient;

    static {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(100);
        cm.setDefaultMaxPerRoute(20);
        cm.setDefaultMaxPerRoute(50);
        httpClient = HttpClients.custom().setConnectionManager(cm).build();
    }

    public static String get(String url) {
        CloseableHttpResponse response = null;
        BufferedReader in = null;
        String result = "";
        try {
            HttpGet httpGet = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000000).setConnectionRequestTimeout(3000000).setSocketTimeout(3000000).build();
            httpGet.setConfig(requestConfig);
            httpGet.setConfig(requestConfig);
            httpGet.addHeader("Content-type", "application/json; charset=utf-8");
            httpGet.setHeader("Accept", "application/json");
            response = httpClient.execute(httpGet);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            result = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != response) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static String post(String url, String jsonString) {
        CloseableHttpResponse response = null;
        BufferedReader in = null;
        String result = "";
        try {
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
            httpPost.setConfig(requestConfig);
            httpPost.setConfig(requestConfig);
            httpPost.addHeader("Content-type", "application/json; charset=utf-8");
            httpPost.setHeader("Accept", "application/json");
            httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
            response = httpClient.execute(httpPost);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
//            String NL = System.getProperty("line.separator");
//            while ((line = in.readLine()) != null) {
//                sb.append(line + NL);
//            }
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            in.close();
            result = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != response) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static void main(String[] args) {

//        String jsonString = "{\"Sid\":\"44030400063005\",\"OrderId\":\"test20190222\",\"Timestamp\":\"2017-12-04 09:56:01\"}";

        //订单信息
//        String mailServer =  "http://gs8grh.natappfree.cc/emsfyxm/servlet/mailserver";

        //订单撤回
//        String deleteUrl = "http://vfzt6m.natappfree.cc/emsfyxm/servlet/DataDelete2";
        //轨迹查询
//        String traceUrl = "http://vfzt6m.natappfree.cc/emsfyxm/servlet/traceRequest";
        String mailServer = "https://uniapi.ys100.com/callapi/LZwNlIzU";
        String emsString = "{\n" +
                "\"accessToken\":\"0a2baf3535eb628049e64452e9c8fa13f7105911\",\n" +
                "\"reqAppCode\":\"1\",\n" +
                "\"reqCharset\":\"\",\n" +
                "\"reqTimestamp\":\"2019-08-27\",\n" +
                "\"reqSignType\":\"\",\n" +
                "\"reqSign\":\"\",\n" +
                "\"reqParam\":{\n" +
                "\"operateMode\":\"UPDATE\",\n" +
                "\"userDtos\":[\n" +
                "{\n" +
                "\"appCode\":\"1\",\n" +
                "\"industry\":\"P\",\n" +
                "\"loginModeList\":[\n" +
                "{\n" +
                "\"loginmodeTerminalId\":\"F12345678\",\n" +
                "\"loginmodeToken\":\"123456\",\n" +
                "\"loginmodeType\":\"字典表SYS-LOGINMODE_TYPE\"\n" +
                "}\n" +
                "],\n" +
                "\"orgList\":[\n" +
                "\"string\"\n" +
                "],\n" +
                "\"positionList\":[\n" +
                "\"string\"\n" +
                "],\n" +
                "\"postList\":[\n" +
                "\"string\"\n" +
                "],\n" +
                "\"postlevelList\":[\n" +
                "\"string\"\n" +
                "],\n" +
                "\"roleList\":[\n" +
                "\"string\"\n" +
                "],\n" +
                "\"sysCoMember\":{\n" +
                "\"coCode\":\"string\",\n" +
                "\"coMemberNo\":\"string\",\n" +
                "\"coMemberEntryDate\":\"2019-07-16T07:01:25.588Z\",\n" +
                "\"coMemberQuitDate\":\"2019-07-16T07:01:25.588Z\",\n" +
                "\"coMemberRetireDate\":\"2019-07-16T07:01:25.588Z\",\n" +
                "\"coMemberType\":\"string\",\n" +
                "\"inServiceStatus\":\"string\"\n" +
                "},\n" +
                "\"userContactInfo\":{\n" +
                "\"userEmail1\":\"string\",\n" +
                "\"userEmail2\":\"string\",\n" +
                "\"userEmail3\":\"string\",\n" +
                "\"userMailAddress\":\"string\",\n" +
                "\"userMailRegion\":\"string\",\n" +
                "\"userMobile1\":\"string\",\n" +
                "\"userMobile2\":\"string\",\n" +
                "\"userMobile3\":\"string\",\n" +
                "\"userPhone1\":\"string\",\n" +
                "\"userPhone2\":\"string\",\n" +
                "\"userPhone3\":\"string\",\n" +
                "\"userQq1\":\"string\",\n" +
                "\"userQq2\":\"string\",\n" +
                "\"userQq3\":\"string\",\n" +
                "\"userWechat1\":\"string\",\n" +
                "\"userWechat2\":\"string\",\n" +
                "\"userWechat3\":\"string\"\n" +
                "},\n" +
                "\"userAccount\":{\n" +
                "\"code\":\"string\"\n" +
                "},\n" +
                "\"userInfo\":{\n" +
                "\"userAddress\":\"string\",\n" +
                "\"userBirthday\":\"2019-07-16T07:01:25.590Z\",\n" +
                "\"userBirthplace\":\"string\",\n" +
                "\"userBirthplaceAddress\":\"string\",\n" +
                "\"userBloodtype\":\"string\",\n" +
                "\"userCountry\":\"string\",\n" +
                "\"userEduDegree\":\"string\",\n" +
                "\"userEduDegreeNo\":\"string\",\n" +
                "\"userEduPeriod\":\"string\",\n" +
                "\"userEduPeriodYear\":\"2019-07-16T07:01:25.590Z\",\n" +
                "\"userEnName\":\"string\",\n" +
                "\"userGatqwm\":\"string\",\n" +
                "\"userGender\":\"string\",\n" +
                "\"userHeight\":0,\n" +
                "\"userHobby\":\"string\",\n" +
                "\"userHomepage\":\"string\",\n" +
                "\"userIdcard\":\"string\",\n" +
                "\"userIdcardType\":\"string\",\n" +
                "\"userJkzmk\":\"string\",\n" +
                "\"userJobTitle\":\"string\",\n" +
                "\"userJobTitleStatus\":\"string\",\n" +
                "\"userMarriage\":\"string\",\n" +
                "\"userName\":\"string\",\n" +
                "\"userName1\":\"string\",\n" +
                "\"userName2\":\"string\",\n" +
                "\"userNation\":\"string\",\n" +
                "\"userOldName\":\"string\",\n" +
                "\"userOldName1\":\"string\",\n" +
                "\"userOldName2\":\"string\",\n" +
                "\"userOriginPlace\":\"string\",\n" +
                "\"userRegResidenceAddress\":\"string\",\n" +
                "\"userRegResidenceRegion\":\"string\",\n" +
                "\"userRegResidenceType\":\"string\",\n" +
                "\"userRegion\":\"string\",\n" +
                "\"userSingleChild\":\"string字典\",\n" +
                "\"userWeight\":0,\n" +
                "\"userWorkCompany\":\"string\",\n" +
                "\"userXyzjm\":\"string\",\n" +
                "\"userZzmmm\":\"string\"\n" +
                "}\n" +
                "}\n" +
                "]\n" +
                "}\n" +
                "}";
        String result = HttpRequestUtil.post(mailServer,emsString);
        System.out.println("===========================:"+result);
    }
}
